package cn.edu.zua.demo04.controller;

import cn.edu.zua.demo04.entity.UserEntity;
import cn.edu.zua.demo04.jpa.UserJPA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 说明：
 *
 * @author ascend
 * 2019/1/23 10:56
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private UserJPA userJPA;

    @Autowired
    public UserController(UserJPA userJPA) {
        this.userJPA = userJPA;
    }

    /**
     * 查询用户列表方法
     *
     * @return List
     */
    @GetMapping("list")
    public List<UserEntity> list() {
        return userJPA.findAll();
    }

    /**
     * 添加、更新用户方法
     *
     * @param entity UserEntity
     * @return UserEntity
     */
    @GetMapping("/save")
    public UserEntity save(UserEntity entity) {
        return userJPA.save(entity);
    }

    /**
     * 删除用户方法
     *
     * @param id 用户编号
     * @return List
     */
    @GetMapping("/delete")
    public List<UserEntity> delete(Long id) {
        userJPA.deleteById(id);
        return userJPA.findAll();
    }
}
